<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/26
 * Time: 16:42
 */

namespace App\Services\LifeCycle;


use App\Models\CarPackageAttrPermission;
use App\Models\CarPackageOrder;
use App\Models\CarPackageOrderAttr;
use App\Models\CarPackageUsedLog;
use App\Models\UserCar;
use Illuminate\Support\Facades\Auth;

class UsedService
{
    public static function detail($used_id)
    {
        return CarPackageUsedLog::find($used_id);
    }

    public static function operatorDetail($used_id)
    {
        $user = Auth::user();

        return CarPackageUsedLog::where('operator_user_id', $user->id)->find($used_id);
    }

    public static function attrSelect($order_id, $user_id)
    {
        $attr = [];
        $operator = Auth::user();
        $package_attr_permission_ids = json_decode($operator->package_attr_permission_ids, true);

        $permission = CarPackageAttrPermission::whereIn('id', $package_attr_permission_ids)->get();

        foreach ($permission as $key => $value) {
            $new_permission[$key] = $value['title'];
        }

        $order_attr = CarPackageOrderAttr::where(['order_id' => $order_id, 'user_id' => $user_id])
            ->whereIn('attr_title', $new_permission)
            ->get();

        if ($order_attr) {
            foreach ($order_attr as $key => $value) {
                $attr[$key]['value'] = $value['id'];
                $attr[$key]['text'] = $value['attr_title'];

                if ($value['usable_number'] < 1) {
                    $attr[$key]['disable'] = true;
                    $attr[$key]['text'] = $value['attr_title']."(无可用次数)";
                }
            }
        }

        return $attr;
    }

    public static function create($param = [])
    {
        $user = Auth::user();

        $attr = CarPackageOrderAttr::where(['user_id' => $param['user_id'], 'order_id' => $param['order_id']])->find($param['attr_id']);

        if ($attr->usable_number == 0) {
            return ['code' => 400, 'message' => '该服务属性可用次数已用尽！'];
        }

        if (!$user->store_id) {
            return ['code' => 400, 'message' => '无权限操作！'];
        }

        $order = CarPackageOrder::where('user_id', $param['user_id'])->find($param['order_id']);

        $user_car = UserCar::where('user_id', $param['user_id'])->first();

        CarPackageUsedLog::create([
            'user_id' => $order->user_id,
            'attr_id' => $attr->id,
            'order_id' => $order->id,
            'package_id' => $order->package_id,
            'package_title' => $order->package_title,
            'package_pic' => $order->package_pic,
            'attr_title' => $attr->attr_title,
            'used_at' => date('Y-m-d H:i:s', time()),
            'store_id' => $user->store_id,
            'operator_user_id' => $user->id,
            'operator_pics' => $param['operator_pics'],
            'operator_at' => date('Y-m-d H:i:s', time()),
            'remarks' => $param['remarks'],
            'car_number' => $user_car->car_number,
        ]);

        return ['code' => 200];
    }

    public static function edit($param = [])
    {
        $used_log = CarPackageUsedLog::find($param['used_log_id']);

//        $used_log->store_id = $param['store_id'];
        $used_log->operator_pics = $param['operator_pics'];
        $used_log->remarks = $param['remarks'];

        $used_log->save();
    }
}